package mshop.com.schild_kotlin.fragments

import android.content.pm.ActivityInfo
import android.os.Bundle
import cn.jzvd.JZVideoPlayer
import cn.jzvd.JZVideoPlayerStandard
import com.bumptech.glide.Glide
import kotlinx.android.synthetic.main.fragment_media.*
import mshop.com.schild_kotlin.R
import mshop.com.schild_kotlin.base.BaseFragment
import mshop.com.schild_kotlin.custom_media_player.JZMediaIjkplayer
import mshop.com.schild_kotlin.entitys.PlayInfoBean
import mshop.com.schild_kotlin.network.Http302Util
import okhttp3.Call
import okhttp3.Callback
import okhttp3.Response
import java.io.IOException
import java.util.LinkedHashMap
import kotlin.collections.ArrayList


/**
 * Created by mayi on 2018/5/30.
 * @Autor CaiWF
 * @Email 401885064@qq.com
 * @TODO
 */
class MediaPlayerFiagment : BaseFragment() {


    var thumbImgUrl: String = ""
    var videoPlayUrl = ArrayList<String>()
    var playInfoBean: PlayInfoBean? = null
    var http302Util: Http302Util? = null


    //静态方法
    companion object {
        fun getInstall(bundle: Bundle): MediaPlayerFiagment {
            val fragment = MediaPlayerFiagment()
            fragment.arguments = bundle
            return fragment
        }
    }

    override fun getLayoutResources(): Int {
        return R.layout.fragment_media
    }

    override fun initView() {
        http302Util = Http302Util()
        playInfoBean = arguments?.getParcelable<PlayInfoBean>("playInfoBean")

        playInfoBean?.let {
            //封面
            Glide.with(this).load(it.cover).into(video_player.thumbImageView)
            it.playInfo?.let {
                for (index in it.indices) {
                    toGet302(it[index].name,it[index].urlList[0].url)
                }
            }
        }
    }


    //获取302跳转地址
   private fun toGet302(name: String, url: String) {
        http302Util?.get(url, object : Callback {
            override fun onFailure(call: Call, e: IOException) {
//                Logger.d("onFailure  :" + e.message)
                map.put(name, url)
                toGo()
            }

            @Throws(IOException::class)
            override fun onResponse(call: Call, response: Response) {
//                Logger.d("onResponse  :" + response.header("Location")!!)
                val location: String = response.header("Location") ?: url
                map.put(name, location)
                toGo()
            }
        })
    }


    val map = LinkedHashMap<String, String>()
    var resultRequestCount: Int = 0
    private fun toGo() {
        resultRequestCount = resultRequestCount + 1

        playInfoBean?.let {
            it.playInfo?.let {
                if (resultRequestCount < it.size) return

//        map.put("高清1080P", "http://lecloud.educdn.huan.tv/mediadns/ts/AK/CDN2016051800171.mp4")
//        map.put("标清720P", "http://lecloud.educdn.huan.tv/mediadns/ts/AK/CDN2016051800171.mp4")
//        map.put("普清360P", "http://lecloud.educdn.huan.tv/mediadns/ts/AK/CDN2016051800171.mp4")
//                Logger.d("Thread name:"+Thread.currentThread().name)
                video_player.post {//切换到主线程
                    JZVideoPlayer.setMediaInterface(JZMediaIjkplayer())
                    val objects = arrayOfNulls<Any>(3)
                    objects[0] = map    //播放地址
                    objects[1] = false  //是否循环播放
//                objects[2] = otherDataMap  //播放进度
                    video_player.setUp(objects, it.size - 1,
                            JZVideoPlayerStandard.SCREEN_WINDOW_NORMAL,
                            playInfoBean?.title ?: "")
                    JZVideoPlayer.FULLSCREEN_ORIENTATION = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
                    JZVideoPlayer.NORMAL_ORIENTATION = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
                }
            }


        }
    }

    override fun onPause() {
        super.onPause()
        JZVideoPlayer.releaseAllVideos()
    }

    override fun onRefresh() {

    }

    override fun getNeedRefresh(): Boolean {
        return false
    }


}